package com.cup.bddm.util;

import org.apache.commons.lang.StringUtils;

import com.cup.bddm.api.entity.MerchantEntity;
import com.cup.bddm.api.entity.StreetEntity;

public class StrUtil {
	/**
	 * 分公司名称-短名称
	 */
	public static Object branchShotName(String cup_branch_nm) {
		if (StringUtils.isEmpty(cup_branch_nm)) {
			return "";
		}
		return cup_branch_nm.replace("中国银联股份有限公司", "").replace("分公司", "");
	}

/*	static HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
	static {
		pinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
		pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
		pinyinFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
	}

	public static String pinyinEncode(String str) {
		char[] arr = str.trim().toCharArray();
		StringBuilder sb = new StringBuilder();
		try {
			for (int i = 0; i < arr.length; i++) {
				if (String.valueOf(arr[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
					String[] ret = PinyinHelper.toHanyuPinyinStringArray(arr[i], pinyinFormat);
					if (ret.length == 0) {
						sb.append(" ").append(arr[i]).append(" ");
					} else {
						sb.append(" ").append(ret[0]).append(" ");
					}
				} else {// 如果字符不是中文,则不转换
					sb.append(arr[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("[" + str + "]不能转成汉语拼音");
		}
		return sb.toString().replaceAll("  ", " ").trim();
	}*/

	public static String indexEncode(String str) {
		char[] arr = str.trim().toCharArray();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			if (String.valueOf(arr[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为区位码
				String ret = quweima(arr[i]);
				sb.append(" ").append(ret).append(" ");
			} else {// 如果字符不是中文,则不转换
				sb.append(arr[i]);
			}
		}
		return sb.toString().replaceAll("  ", " ").trim();
	}

	public static String quweima(char ch) {
		String s = "";
		try {
			byte[] bs = String.valueOf(ch).getBytes("GB2312");
			for (int i = 0; i < bs.length; i++) {
				byte[] b = new byte[] { bs[i] };
				String ret = "";
				for (int j = 0; j < b.length; j++) {
					String hex = Integer.toHexString(b[j] & 0xFF);
					if (hex.length() == 1) {
						hex = '0' + hex;
					}
					ret += hex.toUpperCase();
				}
				int a = Integer.parseInt(ret, 16);
				s += (a - 0x80 - 0x20) + "";
			}
		} catch (Exception e) {
			System.out.println("[" + ch + "]转成区码报错");
		}
		return s;
	}

	public static String getMerchantStoreId(MerchantEntity entity) {
		return entity.getStreet_cd() + "_" + MD5Util.MD5(entity.getStore_nm());
	}

	public static String getMerchantRowKey(MerchantEntity entity) {
		return entity.getStore_id() + "|" + entity.getMchnt_cd() + "|" + entity.getTerm_id();
	}

	public static String getStreetRowKey(StreetEntity entity) {
		return entity.getStreet_cd() + "_" + entity.getService_mchnt_cd();
	}
}
